package main

import (
	"fmt"
)

//#link: https://leetcode-cn.com/problems/set-mismatch/
func main() {
	nums := []int{1, 3, 3}
	fmt.Println(findErrorNums(nums))
}

func findErrorNums(nums []int) []int {
	//利用 2n个数字进行异或最终 得到重复的数和缺失的数 xor:=x^y
	xor := 0
	for _, num := range nums {
		xor ^= num
	}
	for i := 1; i <= len(nums); i++ {
		xor ^= i
	}
	//获取到最低位，将两个数进行分组
	lowBit := xor & (-xor)
	num1, num2 := 0, 0
	for _, num := range nums {
		if num&lowBit == 0 {
			num1 ^= num
		} else {
			num2 ^= num
		}
	}
	for i := 1; i <= len(nums); i++ {
		if i&lowBit == 0 {
			num1 ^= i
		} else {
			num2 ^= i
		}
	}
	//确定nums1 是否是重复的数

	for _, num := range nums {
		if num1 == num {
			return []int{num1, num2}
		}
	}
	return []int{num2, num1}
}
